//
//  ViewController.m
//  City
//
//  Created by cl on 15/10/29.
//  Copyright (c) 2015年 LS. All rights reserved.
//

#import "ViewController.h"
#import "NextViewController.h"
static NSString * cellID = @"cellID";
static int num = 0;
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString * path = [[NSBundle mainBundle] pathForResource:@"ProvincesAndCities" ofType:@"plist"];
    NSArray * cityArr = [NSArray arrayWithContentsOfFile:path];
//     NSLog(@"---cityArr----%@", cityArr);
    
    [self createUI];
    
    self.provinceArr = [NSMutableArray array];
    
    self.cityArr = [NSMutableArray array];
    
    self.citiesArr = [NSMutableArray array];
    
    for (NSDictionary * dic in cityArr)
    {
        // NSLog(@"---Cities---%@", dic[@"Cities"]);
        [self.provinceArr addObject:dic[@"State"]];
        
        NSArray * arr = dic[@"Cities"];
        // self.cityArr中方的是数组  数组中放得是字典
        [self.cityArr addObject:arr];
    }
}
- (void)createUI
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeWords;
    self.tableView.tableHeaderView = self.searchBar;
    
    
    self.displayC = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
    
    //需要设置displayCtr的代理为当前视图控制对象
    //说白了 就是设置displayCtr上的表格视图的代理为当前视图控制器对象
    self.displayC.searchResultsDataSource = self;
    self.displayC.searchResultsDelegate = self;
    
    
    //<5>初始化搜索数组对象
    self.searchArr = [[NSMutableArray alloc]init];
}

#pragma mark -------- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // 分区
    if (tableView == self.tableView) {
        return self.provinceArr.count;
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return [self.cityArr[section] count];
    }
    else
    {
        //<1>将搜索结果数组的内容进行清空
        [self.searchArr removeAllObjects];
        //<2>获取搜索框的内容
        NSString * string = self.searchBar.text;
        //<3>模糊查询
        for(NSArray * arr in self.cityArr)
        {
            for (NSDictionary * dic in arr)
            {
                NSString * bigString = [dic objectForKey:@"city"];
                NSRange range = [bigString rangeOfString:string];
                if(range.location != NSNotFound)
                {
                    //将模糊查询的结果添加到searchArr中
                    [_searchArr addObject:bigString];
                }
            }
        }
        
        return _searchArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        NSLog(@"%d", ++num);
    }
    NSDictionary * dic = self.cityArr[indexPath.section] [indexPath.row];
    if (tableView == self.tableView) {
        cell.textLabel.text = dic[@"city"];
    }
    else
    {
        cell.textLabel.text = [self.searchArr objectAtIndex:indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NextViewController * nextVC = [[NextViewController alloc] init];
    if (tableView == self.tableView) {
        NSDictionary * dic = self.cityArr[indexPath.section] [indexPath.row];
        nextVC.url = dic[@"city"];
    }
    else
    {
        nextVC.url = [self.searchArr objectAtIndex:indexPath.row];
    }
    [self presentViewController:nextVC animated:YES completion:nil];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.provinceArr[section];
    }
    else
    {
    return @"搜索结果";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
